package com.ss.web.tagmark.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.ss.web.tagmark.entity.SystemParameter;
import com.ss.web.tagmark.filter.CookieBasedMessageFilter;

public abstract class BaseController {
	protected Logger logger = LoggerFactory.getLogger("sys");
	protected String messageKey = "msg";
	private static String FLASH_MSG_KEY = "_flash_msg_";

	protected void renderJson(Object obj, HttpServletResponse response) {
		try {
			response.setStatus(HttpServletResponse.SC_OK);
			response.setContentType("application/json; charset=utf-8");
			response.getWriter().write((obj instanceof String) ? (String) obj : JSON.toJSONString(obj));
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void renderHtml(Object obj, HttpServletResponse response) {
		try {
			if (obj == null) {
				obj = "";
			}
			response.setStatus(HttpServletResponse.SC_OK);
			response.setContentType("text/html; charset=utf-8");
			response.getWriter().write((obj instanceof String) ? (String) obj : JSON.toJSONString(obj));
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 跳转到指定的url。
	 *
	 * @param url
	 * @return
	 */
	protected String redirect(String url) {
		return "redirect:" + url;
	}

	/**
	 * 将指定的信息存储到cookie中，用于在跳转时存储提示信息
	 *
	 * @param tips
	 */
	protected void addFlashMsg(HttpServletResponse response, String tips) {
		try {
			tips = Base64.encodeBase64String(tips.getBytes("utf-8"));
		} catch (Exception e) {
		}
		CookieBasedMessageFilter.setCookieValue(FLASH_MSG_KEY, tips);
	}

	/**
	 * 获取cookie中的提示信息
	 *
	 * @param request
	 * @return
	 */
	public static String getFlashMsg(HttpServletRequest request, HttpServletResponse response) {
		Cookie[] cs = request.getCookies();
		Cookie flash = null;
		if (cs != null && cs.length > 0) {
			for (Cookie c : cs) {
				if (FLASH_MSG_KEY.equals(c.getName())) {
					flash = c;
					break;
				}
			}
		}
		if (flash != null) {// 返回的同时，将cookie清空
			String ret = flash.getValue();
			CookieBasedMessageFilter.setCookieValue(FLASH_MSG_KEY, "");
			try {
				return new String(Base64.decodeBase64(ret), "utf-8");
			} catch (Exception e) {
				return ret;
			}
		}
		return "";
	}

	/**
	 * 在指定的参数前面加一个“全部”选项
	 *
	 * @param params
	 * @return
	 */
	protected List<SystemParameter> appendAllOption(List<SystemParameter> params) {
		if (params == null) {
			params = new ArrayList<>();
		}
		SystemParameter all = new SystemParameter();
		all.setLabel("全部");
		all.setValue("");
		params.add(0, all);
		return params;
	}

}
